package com.xoa.controller.crm;


import com.xoa.model.crm.Linkman;
import com.xoa.service.crm.LinkManService;
import com.xoa.util.ToJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/linkMan")
public class LinkManController {

    @Autowired
      private LinkManService linkManService;

    /**
     * 根据客户id查询联系人
     */
    @RequestMapping("selectBylinkman")
    @ResponseBody
    public ToJson<Integer> selectBylinkman(HttpServletRequest request,Integer customerId){
        ToJson json=new ToJson<>(1,"err");
        try {
            List<Linkman> data = linkManService.selectBylinkman(customerId);
            if (data!=null){
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 添加联系人
     * @param request
     * @param linkman
     * @return
     */
    @RequestMapping("addLinkMan")
    @ResponseBody
    public ToJson<Integer> addLinkMan(HttpServletRequest request, Linkman linkman,Integer customerId){
        ToJson json=new ToJson<>(1,"err");
        try {
            Integer data= linkManService.addLinkMan(linkman,customerId);
            if (data!=null){
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 根据客户id回显联系人
     */
    @RequestMapping("selectCustomerById")
    @ResponseBody
    public ToJson<Linkman> selectlinkmanById(HttpServletRequest request,Integer customerId){
        ToJson json=new ToJson<>(1,"err");
        try {
            Linkman data = linkManService.selectlinkmanById(customerId);
            if (data!=null){
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    /**
     * 根据客户id回显联系人及客户信息
     */
    @RequestMapping("selectcustomerByLId")
    @ResponseBody
    public ToJson<Linkman> selectcustomerByLId(HttpServletRequest request,Integer customerId){
        ToJson json=new ToJson<>(1,"err");
        try {
            Linkman data = linkManService.selectcustomerByLId(customerId);
            if (data!=null){
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 根据联系人id回显信息
     */
    @RequestMapping("selectByPrimaryKey")
    @ResponseBody
    public ToJson<Linkman> selectByPrimaryKey(HttpServletRequest request,Integer linkmanId){
        ToJson json=new ToJson<>(1,"err");
        try {
            Linkman data = linkManService.selectByPrimaryKey(linkmanId);
            if (data!=null){
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 编辑联系人
     */
    @RequestMapping("updateLinkMan")
    @ResponseBody
    public ToJson<Integer> updateLinkMan(HttpServletRequest request,Linkman linkman){
        ToJson json=new ToJson<>(1,"err");
        try {
            Integer data = linkManService.updateLinkMan(linkman);
            if (data!=null){
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 删除联系人
     */
    @RequestMapping("deleteLinkMan")
    @ResponseBody
    public ToJson<Integer> deleteLinkMan(HttpServletRequest request,Integer linkmanId){
        ToJson json=new ToJson<>(1,"err");
        try {
            Integer data = linkManService.deleteLinkMan(linkmanId);
            if (data!=null){
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
}
